package de.eridea.learning.java.usermanagement.persistence;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.JoinColumn;

@Entity
@Table(name = "USERS")
public class User {
	@Id
	@GeneratedValue
	private long id;

	@Column(unique = true, nullable = false)
	private String loginName;

	@Column(unique = true)
	private String displayName;

	private String passwordHash;

	@ManyToMany
	@JoinTable(name = "MEMBERSHIP", joinColumns = @JoinColumn(name = "User_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "Group_id", referencedColumnName = "id"))
	private Set<Group> groups = new HashSet<Group>();

	@OneToMany(mappedBy = "owner")
	private Set<Group> ownedGroups = new HashSet<Group>();

	@OneToMany(mappedBy = "owner")
	private Set<Resource> ownedResources = new HashSet<Resource>();

	public long getId() {
		return id;
	}

	private void setId(long id) {
		this.id = id;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		if (loginName == null) {
			throw new IllegalArgumentException("loginName must not be null");
		}
		this.loginName = loginName;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public String getPasswordHash() {
		return passwordHash;
	}

	public void setPasswordHash(String passwordHash) {
		this.passwordHash = passwordHash;
	}

	public Set<Group> getGroups() {
		return groups;
	}

	public void setGroups(Set<Group> groups) {
		this.groups = groups;
	}

	public Set<Group> getOwnedGroups() {
		return ownedGroups;
	}

	public void setOwnedGroups(Set<Group> ownedGroups) {
		this.ownedGroups = ownedGroups;
	}

	public Set<Resource> getOwnedResources() {
		return ownedResources;
	}

	public void setOwnedResources(Set<Resource> ownedResources) {
		this.ownedResources = ownedResources;
	}

}
